﻿Public Class Magento

    Public header As String
    Public isMagentoFileLoaded As Boolean

    Private M_SKU As Integer
    Private M_STATUS As Integer
    Private M_NAME As Integer
    Private M_QTY As Integer
    Private M_PRICE As Integer
    Private M_COST As Integer
    Private M_DESC As Integer
    Private M_SHRTDESC As Integer
    Private M_ISINSTOCK As Integer
    Private openFileDialog1 As New OpenFileDialog()


    Public Property SKU() As Integer
        Get
            Return M_SKU
        End Get
        Set(ByVal value As Integer)
            M_SKU = value
        End Set
    End Property
    Public Property STATUS() As Integer
        Get
            Return M_STATUS
        End Get
        Set(ByVal value As Integer)
            M_STATUS = value
        End Set
    End Property
    Public Property NAME() As Integer
        Get
            Return M_NAME
        End Get
        Set(ByVal value As Integer)
            M_NAME = value
        End Set
    End Property
    Public Property QTY() As Integer
        Get
            Return M_QTY
        End Get
        Set(ByVal value As Integer)
            M_QTY = value
        End Set
    End Property
    Public Property PRICE() As Integer
        Get
            Return M_SKU
        End Get
        Set(ByVal value As Integer)
            M_SKU = value
        End Set
    End Property
    Public Property COST() As Integer
        Get
            Return M_COST
        End Get
        Set(ByVal value As Integer)
            M_COST = value
        End Set
    End Property
    Public Property DESCRIPTION() As Integer
        Get
            Return M_DESC
        End Get
        Set(ByVal value As Integer)
            M_DESC = value
        End Set
    End Property
    Public Property SHORT_DESCRIPTION() As Integer
        Get
            Return M_SHRTDESC
        End Get
        Set(ByVal value As Integer)
            M_SHRTDESC = value
        End Set
    End Property
    Public Property ISINSTOCK() As Integer
        Get
            Return M_ISINSTOCK
        End Get
        Set(ByVal value As Integer)
            M_ISINSTOCK = value
        End Set
    End Property
    Public ReadOnly Property getHeaderAsTAB() As String
        Get
            Return header
        End Get
    End Property
    Public ReadOnly Property getHeaderAsCSV() As String
        Get
            Return header.Replace(CChar(vbTab), CChar(","))
        End Get
    End Property
  


    Sub New()
        Try
            openFileDialog1.Filter = "Magento|*.csv"
            openFileDialog1.Title = "Open Magento Data File..."
            openFileDialog1.InitialDirectory = "c:\magento"

            If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                isMagentoFileLoaded = True
                setHeader()
            Else
                isMagentoFileLoaded = False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Sub New(ByVal MagentoFileName As String)
        Try
            openFileDialog1.Filter = "Magento|*.csv"
            openFileDialog1.Title = "Open Magento Data File..."
            openFileDialog1.InitialDirectory = "c:\magento"

            If My.Computer.FileSystem.FileExists(MagentoFileName) Then
                openFileDialog1.FileName = MagentoFileName
                isMagentoFileLoaded = True
                setHeader()
            Else
                isMagentoFileLoaded = False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Function getFileName() As String
        Return openFileDialog1.FileName
    End Function

    Private Sub setHeader()
        Dim tempArray() As String
        Dim X As Integer

        tempArray = System.IO.File.ReadAllLines(openFileDialog1.FileName)
        Me.header = tempArray(0)

        If Me.header.Contains(vbTab) Then
            If Me.header.Contains("name") And Me.header.Contains("sku") Then
                tempArray = tempArray(0).Split(CChar(vbTab))

                Do While X < tempArray.Length
                    Select Case tempArray(X)
                        Case "sku", """sku"""
                            Me.M_SKU = X
                        Case "status", """status"""
                            Me.M_STATUS = X
                        Case "name", """name"""
                            Me.M_NAME = X
                        Case "qty", """qty"""
                            Me.M_QTY = X
                        Case "price", """price"""
                            Me.M_PRICE = X
                        Case "cost", """cost"""
                            Me.M_COST = X
                        Case "description", """description"""
                            Me.M_DESC = X
                        Case "short_description", """short_description"""
                            Me.M_SHRTDESC = X
                        Case "is_in_stock", """is_in_stock"""
                            Me.M_ISINSTOCK = X
                        Case Else
                            'SKIP
                    End Select
                    X += 1
                Loop
            Else
                MessageBox.Show("This does not seem like a valid MAGENTO exported file. Program will now terminate!")
                End
            End If
        Else
            MessageBox.Show("This is not a TAB deliminated CSV file. Program will now terminate!")
            End
        End If

    End Sub
End Class

